<%@page import="org.brickred.socialauth.SocialAuthManager"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.brickred.socialauth.AuthProvider"%>
<%@page import="org.brickred.socialauth.util.SocialAuthUtil"%>
<%@page import="java.util.Map"%>
<%@page import="org.brickred.socialauth.Profile"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.brickred.socialauth.Contact"%>
<%@page import="java.util.List"%>

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
	List<Contact> contactsList = new ArrayList<Contact>();
	
	Profile profile = null;
	SocialAuthManager manager = (SocialAuthManager) session.getAttribute("manager");
	Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
	if(session.getAttribute("contactsList")==null){
		AuthProvider provider = manager.connect(paramsMap);
		profile = provider.getUserProfile();
		contactsList = provider.getContactList();
	}else{
		contactsList = (List<Contact>) session.getAttribute("contactsList");		
	}
	session.setAttribute("contactsList", contactsList);
	Connection con = null;
	Statement st = null;
	Class.forName("org.gjt.mm.mysql.Driver");
	con = DriverManager.getConnection("jdbc:mysql://23.23.218.35/valcomadmin","valcomadmin","ValCom2000!");
	st = con.createStatement();
	String accountNo = session.getAttribute("acno") + "";
	//out.println("accountNo  " + accountNo);
	String msg = "";
	
	int recordInserted = 0;
	if (contactsList != null && contactsList.size() > 0) {
		for (Contact contact : contactsList) {
			//out.println("Email..." + contact.getEmail());
					String selectQuery = "select * from vcaccount_addressbook where Account_Number='"+ accountNo +"' and Email_Address='"+ contact.getEmail() +"'";
					//out.println(selectQuery + "<br>");
					ResultSet rsContacts = st.executeQuery(selectQuery);
					
					if(rsContacts!=null && rsContacts.next()){
						//out.println("COme to update  <br>");
						String friend_Status = rsContacts.getString("Friend_Status");
						if(!friend_Status.equals("1")){
							String updateQuery = "update vcaccount_addressbook set First_Name='"+ contact.getFirstName() +"', Family_Name='"+ contact.getLastName() +"', Friend_Status='1', Connection_Date=now() where Account_Number='"+ accountNo +"' and Email_Address='"+ contact.getEmail() +"'";
							st.executeUpdate(updateQuery);
							recordInserted++;
						}
					}else{
						String insertQuery = "insert into vcaccount_addressbook (Account_Number, First_Name, Family_Name, Email_Address, Friend_Status, Connection_Date) values ('"+ accountNo +"', '"+ contact.getFirstName() +"', '"+ contact.getLastName() +"', '"+ contact.getEmail() +"', 1,now())";
						//out.println("Quey..."  + insertQuery + "<br>");
						st.executeUpdate(insertQuery);
						recordInserted++;
					}
				}
	}
	msg = recordInserted + " of your " + session.getAttribute("service") +" contacts are successfully imported to your VCAhha contact book";
	if (!session.isNew()) {
		session.invalidate();
	}
%>
	<input type="hidden" id="msg" name="msg" value="<%=msg%>">
	<script>

		alert(document.getElementById('msg').value);
		window.location="http://23.23.78.157/index.php?option=com_content&view=article&id=7&Itemid=150";

</script>
</body>
</html>